package com.hcss.core.mvc;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.hcss.core.common.permissions.model.UserInfo;
import com.hcss.core.mvc.page.Page;
import com.hcss.core.mvc.page.Pager;
import com.hcss.core.util.SysDictionary;


/**
 * @ClassName: BaseController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author lvfang
 * @date 2013-3-27 上午9:19:13
 * 
 */
public class BaseController {
	public final Logger logger = Logger.getLogger(getClass());
	public HttpServletRequest serverSequest;

	/**
	 * 获取用户
	 * 
	 * @return
	 */
	protected UserInfo getUserInfo() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        serverSequest = request;
        return (UserInfo) request.getSession().getAttribute(SysDictionary.LOGIN_USERINFO);
	}

	/**
	 * @Title: getMsgTip
	 * @Description: TODO 返回提示信息
	 * @param state
	 *            操作状态
	 * @return Map<String,Object> 返回类型
	 * @throws
	 */
	public Map<String, String> getMsgTip(int state) {
		Map<String, String> map = new HashMap<String, String>();
		if (state == 1) {
			map.put("info", "success");
		}
		else if(state == 503){
			map.put("info", "503");
		}
		else if(state == 502){
			map.put("info", "502");
		}
		else if(state == 504){
			map.put("info", "504");
		}
		else if(state == 400){
			map.put("info", "400");
		}
		else if(state == 31){
			map.put("info", "31");
		}
		else if(state == 41){
			map.put("info", "41");
		}
		else{
			map.put("info", "failure");
		}
		return map;
	}
	
	/**
	 * @Title: getMsgTip
	 * @Description: TODO 返回提示信息
	 * @param state
	 *            操作状态
	 * @return Map<String,Object> 返回类型
	 * @throws
	 */
	public Map<String, String> getMsgTip(String messageKey) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("info", messageKey);
		return map;
	}
	
	/**
	 * @Title: getMsgTip
	 * @Description: TODO 返回提示信息
	 * @param state
	 *            操作状态
	 * @return Map<String,Object> 返回类型
	 * @throws
	 */
	public Map<String, String> getMsgTip(int state,Map<String, String> map) {
		if (state > 0) {
			map.put("info", "success");
		} else {
			map.put("info", "failure");
		}
		return map;
	}
	/**
	 * @Title: getMsgTip
	 * @Description: TODO 返回提示信息
	 * @param state
	 *            操作状态
	 * @return Map<String,Object> 返回类型
	 * @throws
	 */
	public Map<String, String> getMsgTipTwo(int state) {
		Map<String, String> map = new HashMap<String, String>();
		if (state == 1) {
			map.put("info", "success");
		}else if(state == 2){
			map.put("info", "flowFailure");
		}else if(state == 503){
			map.put("info", "NO_NEXT_OPERATOR");
		}else if(state == 601){
			map.put("info", "NO_NEXT_OPERATOR");
		}else if(state == 701){
			map.put("info", "NO_NEXT_OPERATOR_701_ESG");
		}else if(state == 702){
			map.put("info", "NO_NEXT_OPERATOR_702_ESG");
		}else if(state == 703){
			map.put("info", "NO_NEXT_OPERATOR_703_ESG");
		}else{
			map.put("info", "failure");
		}
		return map;
	}
	/**
	 * @Title: getMsgTip
	 * @Description: TODO 返回提示信息
	 * @param state
	 *            操作状态
	 * @return Map<String,Object> 返回类型
	 * @throws
	 */
	public Map<String, String> getMsgTipStart(int result) {
		Map<String, String> map = new HashMap<String, String>();
		String msg = "failure";
		if(result == 1){
			msg="success";
		}else if(result ==2){
			msg="startSuccess";
		}else if(result ==3){
			msg="flowFailure";
		}
		map.put("info", msg);
		return map;
	}
	/**
	 * @Title: getMsgTipInfo
	 * @Description: 验证是否重复、是否可以被禁用(正在使用不能被禁用)
	 * @param state
	 *            操作状态
	 * @return Map<String,Object> 返回类型
	 * @throws
	 */
	public Map<String, String> getMsgTipInfo(int state) {
		Map<String, String> map = new HashMap<String, String>();
		if (state == 1) {
			map.put("info", "success");
		} else if (state == 2) {
			map.put("info", "active");
		} else {
			map.put("info", "failure");
		}
		return map;
	}
	
	/**
	 * 验证登录成功/失败
	 * @Title: getMsgTip
	 * @Description: TODO 返回提示信息
	 * @param state 操作状态
	 * @return Map<String,Object> 返回类型
	 * @throws
	 */
	public Map<String, String> getMsgTipByLoginStatus(int status) {
		Map<String, String> map = new HashMap<String, String>();
		if (status == 1) {
			map.put("info", "success");
		}else if(status == 2) {
			map.put("info", "locked");
		}else if(status == 3){
			map.put("info", "leavejob");
		}else  {
			map.put("info", "failure");
		}
		return map;
	}

	/**
	 * 获取登录用户id
	 * 
	 * @param request
	 * @return String
	 */
	public String getUserId(HttpServletRequest request) {
		String userId = "";
		Object obj = request.getSession().getAttribute(
				SysDictionary.LOGIN_USERINFO);
		if (obj != null) {
			UserInfo userInfo = (UserInfo) obj;
			userId = String.valueOf(userInfo.getId());
		}
		return userId;
	}

	/**
	 * 获取登录用户编号userNo
	 * 
	 * @param request
	 * @return String
	 */
	public String getUserNo(HttpServletRequest request) {
		String userNo = "";
		Object obj = request.getSession().getAttribute(
				SysDictionary.LOGIN_USERINFO);
		if (obj != null) {
			UserInfo userInfo = (UserInfo) obj;
			userNo = userInfo.getUserNo();
		}
		return userNo;
	}

	/**
	 * 获取登录用户角色id，如(1,2,3)
	 * 
	 * @param request
	 * @return String
	 */
	public String getUserRoleInfos(HttpServletRequest request) {
		String userRoles = "";
		Object obj = request.getSession().getAttribute(
				SysDictionary.LOGIN_USERINFO);
		if (obj != null) {
			//UserInfo userInfo = (UserInfo) obj;
			//userRoles = userInfo.getRoleIds();
		}
		return userRoles;
	}
	/**
	 * @Title: getPager
	 * @Description: TODO 获取当前分页对象 包含分页参数
	 * @param queryParams
	 *            查询参数
	 * @return Pager 返回类型
	 * @throws
	 */
	public Pager getPager(Map<String, Object> queryParams)
			throws IllegalAccessException, InvocationTargetException {
		Page page = new Page(getUserInfo());
		BeanUtils.populate(page, queryParams);
		return Page.toPager(page);
	}
	/**
	 * @Title: getPager
	 * @Description: TODO 获取当前分页对象 包含分页参数
	 * @param queryParams
	 *            查询参数
	 * @return Pager 返回类型
	 * @throws
	 */
	public Pager getPagerSetPageSize(Map<String, Object> queryParams,int pageSize)
			throws IllegalAccessException, InvocationTargetException {
		Page page = new Page(getUserInfo(),pageSize);
		BeanUtils.populate(page, queryParams);
		return Page.toPager(page);
	}
	/**
	 * @Title: getCurrentPager
	 * @Description: TODO 返回分页数据集合 包含分页信息
	 * @param pages
	 *            当前pages对象
	 * @return Map<String,Object> 返回类型
	 * @throws
	 */
	public Map<String, Object> getCurrentPager(Pager pages) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", pages.getTotalRows());
		result.put("rows", pages.getDataList());
		return result;
	}
	
	/**
	 * @Title: getCurrentPager
	 * @Description: TODO 返回分页数据集合 包含分页信息
	 * @param pages
	 *            当前pages对象
	 * @return Map<String,Object> 返回类型
	 * @throws
	 */
	public Map<String, Object> getCurrentPagerTotal(Pager pages) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", pages.getTotalRows());
		result.put("rows", pages.getDataList());
		result.put("footer", pages.getFooterList());
		return result;
	}
	 /**
     * 验证登录成功/失败
     * 
     * @Title: getMsgTip
     * @Description: TODO 返回提示信息
     * @param state 操作状
     * @return Map<String,Object> 返回类型
     * @throws
     */
    public Map<String, String> getMsgTipByLoginStatus(int status,HashMap<String, String> map) {
        if (status == 1) {
            map.put("info", "success");
        } else if (status == 2) {
            map.put("info", "locked");
        } else if (status == 3) {
            map.put("info", "authCodeError");
        } else {
            map.put("info", "failure");
        }
        return map;
    }
}
